package com.lt.bcra.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lt.bcra.mapper.BcraLifeHabitsMapper;
import com.lt.bcra.entity.BcraLifeHabits;
import com.lt.bcra.exception.BaseException;
import com.lt.bcra.service.BcraLifeHabitsService;
import com.lt.bcra.result.ResponseEnum;
import com.lt.bcra.result.ResultData;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 生活习惯表 服务实现类
 * </p>
 *
 * @author LuoTian
 * @since 2022-05-25
 */
@Service
public class BcraLifeHabitsServiceImpl extends ServiceImpl<BcraLifeHabitsMapper, BcraLifeHabits> implements BcraLifeHabitsService {

    @Autowired
    private BcraLifeHabitsMapper bcraLifeHabitsMapper;


    @Override
    public ResultData saveModule(BcraLifeHabits bcraLifeHabits) {

        bcraLifeHabits.setModuleId(3);
        bcraLifeHabits.setModuleTitle("生活习惯表");

        int ok = bcraLifeHabitsMapper.insert(bcraLifeHabits);
        if (ok != 1) {
            throw new BaseException(ResponseEnum.CREATE_DATA_ERROR);
        }
        return ResultData.getResult(ok);
    }

    @Override
    public ResultData removeByQuesCode(String questionnaireCode) {
        QueryWrapper<BcraLifeHabits> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("questionnaire_code", questionnaireCode);
        int ok = bcraLifeHabitsMapper.delete(queryWrapper);
        if (ok != 1) {
            throw new BaseException(ResponseEnum.DELETE_DATA_ERROR);
        }
        return ResultData.getResult(ok);
    }

    @Override
    public ResultData updateModule(BcraLifeHabits bcraLifeHabits) {
        QueryWrapper<BcraLifeHabits> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("questionnaire_code",bcraLifeHabits.getQuestionnaireCode());
        int ok = bcraLifeHabitsMapper.update(bcraLifeHabits, queryWrapper);
        if (ok != 1) {
            throw new BaseException(ResponseEnum.UPDATE_DATA_ERROR);
        }
        return ResultData.getResult(ok);
    }

    @Override
    public ResultData selectByQuesCode(String questionnaireCode) {
        QueryWrapper<BcraLifeHabits> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("questionnaire_code", questionnaireCode);
        BcraLifeHabits bcraLifeHabits = bcraLifeHabitsMapper.selectOne(queryWrapper);
        if (bcraLifeHabits == null) {
            throw new BaseException(ResponseEnum.SELECT_DATA_ERROR);
        }
        return ResultData.getResult(bcraLifeHabits);

    }
}
